package cn.ym.mall.service.impl;

import cn.ym.mall.dao.GoodsActiveJoinDao;
import cn.ym.mall.dao.impl.GoodsActiveJoinDaoImpl;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.entity.GoodsActiveJoin;
import cn.ym.mall.service.GoodsActiveJoinService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 */
public class GoodsActiveJoinServiceImpl implements GoodsActiveJoinService {
    GoodsActiveJoinDao goodsActiveJoinDao = new GoodsActiveJoinDaoImpl();
    /**
     * 新增参加活动的商品
     *
     * @param goodsActiveJoin
     * @return
     * @throws SQLException
     */
    @Override
    public boolean addGoodsActiveJoins(GoodsActiveJoin goodsActiveJoin) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (goodsActiveJoinDao.add(goodsActiveJoin) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询所有商品（活动）
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodsActiveJoin> getAll() throws SQLException {
        Connection conn = null;
        List<GoodsActiveJoin> goodsActiveJoinList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            goodsActiveJoinList = goodsActiveJoinDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return goodsActiveJoinList;
    }

    /**
     * 根据id查询商品是否参加活动
     *
     * @param aid
     * @return
     * @throws SQLException
     */
    @Override
    public GoodsActiveJoin getGoodsJoinByAid(Integer aid) throws SQLException {
        Connection conn = null;
        GoodsActiveJoin goodsActiveJoin = null;
        try {
            conn = DruidUtils.getConnection();
            goodsActiveJoin = goodsActiveJoinDao.getGoodsActiveJoinByAid(aid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return goodsActiveJoin;
    }

    /**
     * 分页查询商品（活动）
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<GoodsActiveJoin> getAllGoodsPage(Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<GoodsActiveJoin> page = new Page<GoodsActiveJoin>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)goodsActiveJoinDao.getGoodsActiveJoinsCount());
            page.setList(goodsActiveJoinDao.getGoodsActiveJoinPage(pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }

        return page;
    }
}
